<template>
  <el-dialog v-el-drag-dialog title="关联商品" :visible.sync="visible" :append-to-body="appendToBody" :before-close="onClose">
    <el-card style="margin:0 0 5px 5px">
      <el-button v-permission="['mall:seckill:edit']" class="m-b-10" type="primary" icon="el-icon-plus" @click="handleInfo()">
        新增
      </el-button>
      <el-button v-permission="['mall:seckill:edit']" v-if="selectionData.length > 0" class="m-b-10 m-l-10" type="danger" icon="el-icon-delete" @click="handleDelete()">
        删除
      </el-button>
      <table-toolbar :showSearch.sync="showSearch" @refresh="getTableData"/>
      <el-table
        ref="queryTable"
        v-loading="tableLoading"
        row-key="id"
        :data="tableData"
        element-loading-text="加载中"
        border
        fit
        highlight-current-row
        show-overflow-tooltip
        height="100px"
        v-el-height-adaptive-table="{bottomOffset: 110}"
        @sort-change="sortChange"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="40"/>
        <el-table-column align="center" label="排序" prop="seckillSort" width="45" />
        <el-table-column align="center" label="商品名称" prop="skuName" show-overflow-tooltip />
        <el-table-column align="center" label="秒杀价格" prop="seckillPrice" width="150" show-overflow-tooltip />
        <el-table-column align="center" label="秒杀总量" prop="seckillCount" width="150" show-overflow-tooltip />
        <el-table-column align="center" label="限购数量" prop="seckillLimit" width="80" show-overflow-tooltip />
        <el-table-column align="center" label="操作" width="100" fixed="right">
          <template slot-scope="scope">
            <el-link icon="el-icon-view" @click="handleInfo(scope.row.id)">编辑</el-link>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="tableQuery.total>0" :total="tableQuery.total" :page.sync="tableQuery.page" :limit.sync="tableQuery.limit" @pagination="getTableData"/>

      <seckill-relation-dialog v-if="openFormDialog" ref="relation_form" @confirmForm="handleFilter" />
    </el-card>
  </el-dialog>
</template>

<script>
import { seckillRelationTable, seckillRelationDelete } from '@/api/mall/seckill/relation'
import Pagination from '@/components/Pagination'
import SeckillRelationDialog from './form/dialog'

export default {
  components: {
    Pagination,
    SeckillRelationDialog
  },
  name: 'SeckillRelationTableDialog',
  data() {
    return {
      visible: false,
      appendToBody: false,
      hasLoaded: false,
      showSearch: true,
      queryData: {
        Q_sessionId_L_EQ: undefined
      },
      tableData: [],
      tableLoading: false,
      tableQuery: {
        page: 1,
        limit: 20,
        total: 0,
        sort: undefined,
        order: undefined
      },
      selectionData: [],
      openFormDialog: false
    }
  },
  methods: {
    init(sessionId) {
      this.queryData.Q_sessionId_L_EQ = sessionId
      this.getTableData()
    },
    handleFilter() {
      this.tableQuery.page = 1
      this.getTableData()
    },
    getTableData() {
      const query = Object.assign(this.tableQuery, this.queryData)
      this.tableLoading = true
      seckillRelationTable(query).then(response => {
        this.tableData = response.param.data
        this.tableQuery.total = response.param.recordsTotal
        this.tableLoading = false
        this.$refs.queryTable.bodyWrapper.scrollTop = 0
      }).catch(e => {
        this.tableLoading = false
      })
    },
    sortChange(data) {
      const {prop, order} = data
      this.tableQuery.sort = prop
      if (order === 'ascending') {
        this.tableQuery.order = 'asc'
      } else {
        this.tableQuery.order = 'desc'
      }
      this.getTableData()
    },
    handleSelectionChange(val) {
      this.selectionData = val
    },
    handleInfo(id) {
      this.openFormDialog = true
      this.$nextTick(() => {
        this.$refs.relation_form.init(this.queryData.Q_sessionId_L_EQ, id)
      })
    },
    handleDelete(id) {
      const ids = this.selectionData.map(function (session, index) {
        return session.id
      }).join(',')

      id = id || ids
      if (!id || id.length === 1) {
        this.$modal.msgWarning('请先选中数据项')
        return
      }

      this.$confirm('是否删除所选数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger'
      }).then(() => {
        seckillRelationDelete({ids: id}).then(response => {
          this.handleFilter()
          this.$modal.msgSuccess(response.msg)
        }).catch(e => {
        })
      }).catch(e => {
      })
    }
  }
}
</script>

<style scoped>
.line {
  text-align: center;
}
</style>
